package com.example.chiro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Table_cavite {

	public static final String ROW_ID = "_id";

	public static final String ID_FICHE = "id_fiche";
	public static final String PATH_PHOTO = "path_photo";
	public static final String TYPE = "type";
	public static final String FIBROSCOPE = "fibroscope";
	public static final String COULURE = "coulure";
	public static final String GUANO = "guano";
	public static final String POSITION = "position";
	public static final String ENVOYE = "envoye";

	public static final String DATABASE_TABLE = "cavite";
	private DataBaseHelper DBHelper;
	private SQLiteDatabase DB;
	private final Context context;

	public Table_cavite(Context c) {
		this.context = c;
	}

	private static class DataBaseHelper extends SQLiteOpenHelper {

		public DataBaseHelper(Context context) {
			super(context, DB_FICHEARBRE_adapter.DATABASE_NAME, null,
					DB_FICHEARBRE_adapter.DATABASE_VERSION);
		}

		public void onCreate(SQLiteDatabase arg0) {
		}

		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		}
	}

	public Table_cavite open() throws SQLException {
		this.DBHelper = new DataBaseHelper(this.context);
		this.DB = this.DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		this.DBHelper.close();
	}

	public long inserer_unparun(String id_fiche, String path_photo,
			String type, String fibroscope, String coulure, String guano,
			String position) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(ID_FICHE, id_fiche);
		initialValues.put(PATH_PHOTO, path_photo);
		initialValues.put(TYPE, type);
		initialValues.put(FIBROSCOPE, fibroscope);
		initialValues.put(COULURE, coulure);
		initialValues.put(GUANO, guano);
		initialValues.put(POSITION, position);
		return this.DB.insert(DATABASE_TABLE, null, initialValues);
	}

	public boolean delete_donnees_general(long rowId) {
		return this.DB.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
	}
	
	public boolean update_envoi(int rowId, String etat) {
	    ContentValues args = new ContentValues();
	    args.put(ENVOYE, etat);
	    
	    return this.DB.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) > 0;
	}

	public void inserer_parSlide(ContentValues[] recolte_donnees) {
		for(int i=0;i<recolte_donnees.length;i++)
			this.DB.insert(DATABASE_TABLE, null, recolte_donnees[i]);
	}

	public Cursor getByID(int id){
		Cursor resultat = null;
		resultat = this.DB.query(DATABASE_TABLE, new String[]{ROW_ID,ID_FICHE,PATH_PHOTO,TYPE,FIBROSCOPE,COULURE,GUANO,POSITION,ENVOYE}, ID_FICHE+"=\""+String.valueOf(id)+"\"",null,null,null,null );
		return resultat;
	}

	public int nombre_non_envoye() {
		int c = DB.query(DATABASE_TABLE, new String[] { "_id" }, ENVOYE + "=" + "false", null,
				null, null, null).getCount();
		return c;
	}
	
}
